layui.use(['layer', 'element'], function () {
    var layer = layui.layer;
    var element = layui.element;
    var $ = layui.$;
    var layId;

    var TAB = {
        closed: true,
        openTabNum: 10,
        tabFilter: "tab"
    };

    //显示左侧菜单
    if ($(".navBar").html() == '') {
        var _this = this;
        $.ajax({
            url: '/user/getMenu',
            type: 'post',
            async: false,
            success: function (navs) {
                $(".navBar").html(navBar(navs)).height($(window).height() - 230);
                //初始化页面元素
                element.render();
                $(window).resize(function () {
                    $(".navBar").height($(window).height() - 230);
                });
            }
        });
    }

    layui.define(function (exports) {
        var tabIdIndex = 0;
        var obj = {
            //通过title获取lay-id
            getLayId: function (title) {
                $(".layui-tab-title.top-tab li span").each(function () {
                    if ($(this).text() == title) {
                        layId = $(this).parent().attr("lay-id");
                    }
                });
                return layId;
            },
            //通过title判断tab是否存在
            hasTab: function (title) {
                var tabIndex = -1;
                $(".layui-tab-title.top-tab li span").each(function () {
                    if ($(this).text() == title) {
                        tabIndex = 1;
                    }
                });
                return tabIndex;
            },
            //右侧内容tab操作
            tabAdd: function (_this) {
                if (_this.find("i.layui-icon").attr("data-icon") != undefined) {
                    var title = '';
                    if (obj.hasTab(_this.find("cite").text()) == -1 && _this.siblings("dl.layui-nav-child").length == 0) {
                        if ($(".layui-tab-title.top-tab li").length == TAB.openTabNum) {
                            layer.msg('只能同时打开' + TAB.openTabNum + '个选项卡哦。不然系统会卡的！');
                            return;
                        }
                        tabIdIndex++;
                        title += "<span>" + _this.find("cite").text() + "</span>";
                        title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + tabIdIndex + '">&#x1006;</i>';
                        element.tabAdd(TAB.tabFilter, {
                            title: title,
                            content: "<iframe src='" + _this.attr("data-url") + "' data-id='" + tabIdIndex + "'></frame>",
                            id: new Date().getTime()
                        });
                    }
                    //切换一个Tab项
                    element.tabChange(TAB.tabFilter, obj.getLayId(_this.find("cite").text()));
                }
            }

        };
        //输出tab接口
        exports('tab', obj);
    });


    $("body").on("click", ".top-tab li", function () {
        //切换后获取当前窗口的内容
        element.tabChange(TAB.tabFilter, $(this).attr("lay-id")).init();
    })

    //删除tab
    $("body").on("click", ".top-tab li i.layui-tab-close", function () {
        element.tabDelete(TAB.tabFilter, $(this).parent("li").attr("lay-id")).init();
    })

});